<template>
  <div class="table-container">
    <div class="table-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>员工管理</el-breadcrumb-item>
        <el-breadcrumb-item>员工列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table-search">
      <el-row>
        <el-input
          style="width: 200px;margin-right: 10px;"
          placeholder="请输入员工信息查询"
          v-model="keyword"
          clearable
          @clear="resetData"
        >
        </el-input>
        <el-button type="primary" @click="resetData" >搜索</el-button>
      </el-row>
    </div>
    <div class="table-content">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="未冻结" name="1"></el-tab-pane>
        <el-tab-pane label="已冻结" name="2"></el-tab-pane>
      </el-tabs>
      <el-table
        border
        empty-text="当前数据空空如也"
        ref="multipleTable"
        :data="offerList"
        tooltip-effect="dark"
        style="width: 100%;"
        @select="selectedRow"
        @select-all="selectedAll"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="room_type" label="工号" width="80"></el-table-column>
        <el-table-column prop="realname" label="员工名" width="auto"></el-table-column>
        <!-- <el-table-column prop="username" label="微信号" width="auto"></el-table-column> -->
        <el-table-column prop="mobile" label="手机号" width="auto"></el-table-column>
        <!-- <el-table-column prop="mobile" label="状态" width="auto">
          <template slot-scope="scope">
            <p>{{scope.row.status==1?'正常':scope.row.status==0?'冻结':''}}</p>
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="IFNULL" label="企业微信" width="auto" :formatter="changeQy"></el-table-column> -->
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              style="margin-right: 5px"
              @click="toEdit(scope.$index, scope.row)">编辑
            </el-button> -->
            <el-button type="warning" size="mini" v-if="activeName=='1'" @click="toCanael(scope.$index, scope.row)">冻结</el-button>
            <el-button type="success" size="mini" v-if="activeName=='2'" @click="toCanael(scope.$index, scope.row)">解冻</el-button>
            <!-- <el-button v-if="scope.row.status!=null" @click="toCanael(scope.$index, scope.row)" :type="scope.row.status==1?'warning':'success'" size="mini">{{scope.row.status==1?'冻结':'解冻'}}</el-button> -->
            <el-button slot="reference" @click="delOffer(scope.$index, scope.row)" type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-nav">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 50,100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        page: 1,
        pageSize: 10,
        total: 0,
        keyword: '',
        offerList: [],
        multipleSelection: [],
        activeName:'1'
      }
    },
    mounted() {
      this.queryList();
    },
    methods: {
        changeTime(row, column, cellValue, index){
            if(cellValue){
                return this.$moment(cellValue*1000).format('yyyy-MM-DD HH:ss')
            }
        },
		changeQy(row , column, cellValue,index){
      console.log(cellValue,'qywx');
			if(cellValue!='0'){
				return '有'
			}else{
				return '无'
			}
		},

    // 切换tabs
    handleClick(e){
      if(e.name=='1'){
        this.queryList()
      }else{
        this.getFrozenStaff()
      }
    },

        //   删除按钮
        delOffer(index,row){
            console.log(index,row,'1111');
            this.$confirm('确定要删除该员工吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                this.toDelete(row.user_id)
            }).catch(()=>{})
        },
        toDelete(id_lst) {
            this.$http.post('/api/admin/staff/Index/delStaff', {
                staff_user_id: id_lst,
            }).then(rest => {
                if (rest.code == 1) {
                  if(this.activeName=='1'){
                    this.queryList();
                  }else{
                    this.getFrozenStaff()
                  }
                    this.multipleSelection = [];
                    this.$message.success('删除成功');
                } else {
                    this.$message.error(rest.msg);
                }
            })
        },

        handleBatchDel() {
            if (this.multipleSelection.length < 1) {
                this.$message.error("请先勾选需要删除的员工！");
                return;
            }
            this.$confirm('确定要执行批量删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.toDelete(this.multipleSelection.join(","))
            }).catch(() => {
                console.log('放弃删除')
            });

        },
        selectedRow(selection, row) {
            this.multipleSelection = [];
            const _ths = this;
            selection.map(function (item) {
                _ths.multipleSelection.push(item.id);
            })
        },
        selectedAll(rows) {
            if (rows.length === 0) {
                this.multipleSelection = [];
            } else {
                const _ths = this;
                rows.map(function (item) {
                    if (!_ths.multipleSelection.includes(item.id)) {
                    _ths.multipleSelection.push(item.id);
                    }
                })
            }
        },

        // 编辑按钮
        toEdit(index,row){
          console.log(row,'员工==');
          let data = encodeURIComponent(JSON.stringify(row))
          this.$router.push({name:'staffDetail',query:{data}})
        },

        // 冻结&解冻按钮
        toCanael(index,row){
          console.log(row,'冻结按钮');
          let text = this.activeName=='1'?'确认冻结该员工吗？':'确认解冻该员工吗？';
            this.$confirm(text,'提示',{
                confirmButtonText:'确定',
                cancelButtonText:'取消',
                type:'warning'
            }).then(()=>{
                let params = {
                  staff_user_id:row.user_id
                }
                let url = this.activeName=='1'?'/api/admin/staff/Index/freeze':'/api/admin/staff/Index/unfreeze';
                this.$http.post(url,params).then(res=>{
                    if(res.code==1){
                        this.$message.success('操作成功')
                        if(this.activeName=='1'){
                          this.queryList()
                        }else{
                          this.getFrozenStaff()
                        }
                    }else{
                        this.$message.warning(res.msg)
                    }
                })
            })
        },

        handleSizeChange(val) {
            this.pageSize = val;
            this.page = 1;
            if(this.activeName=='1'){
              this.queryList()
            }else{
              this.getFrozenStaff()
            }
        },
        handleCurrentChange(val) {
            this.page = val;
            if(this.activeName=='1'){
              this.queryList()
            }else{
              this.getFrozenStaff()
            }
        },

        queryList() {
          let params = {
            page: this.page,
            pagesize: this.pageSize,
            keyword: this.keyword,
				    is_staff:1,
          }
            this.$http.post('/api/admin/staff/Index/StaffList',params).then(res => {
				        console.log(res,'员工列表')
                if (res.code == 1) {
                    this.offerList = res.data.list?res.data.list:[];
                    this.total = res.data.total;
                }
            })
        },

        // 冻结员工列表
        getFrozenStaff(){
          let params = {
            page: this.page,
            pagesize: this.pageSize,
            keyword: this.keyword,
				    // is_staff:1,
          }
          this.$http.post('/api/admin/staff/Index/freezeList',params).then(res=>{
            console.log(res,'冻结员工列表');
            if (res.code == 1) {
                this.offerList = res.data.list?res.data.list:[];
                this.total = res.data.total;
            }
          })
        },

        resetData(){
            this.page = 1;
            this.queryList();
        }
    }
  }
</script>
<style lang="less" scoped>
  @import 'index.less';
</style>
